package com.ruoyi.project.wenyanwen.tag.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.project.wenyanwen.tag.domain.ArticleTag;
import com.ruoyi.project.wenyanwen.tag.mapper.ArticleTagMapper;
import com.ruoyi.project.wenyanwen.tag.service.IArticleTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 内容标签Service业务层处理
 *
 * @author shixiangwen
 * @date 2020-08-03
 */
@Service
@DataSource(DataSourceType.KKWENYANWEN)
public class ArticleTagServiceImpl implements IArticleTagService {
    @Autowired
    private ArticleTagMapper articleTagMapper;

    /**
     * 查询内容标签
     *
     * @param tagId 内容标签ID
     * @return 内容标签
     */
    @Override
    public ArticleTag selectArticleTagById(Long tagId) {
        return articleTagMapper.selectArticleTagById(tagId);
    }

    /**
     * 查询内容标签列表
     *
     * @param articleTag 内容标签
     * @return 内容标签
     */
    @Override
    public List<ArticleTag> selectArticleTagList(ArticleTag articleTag) {
        return articleTagMapper.selectArticleTagList(articleTag);
    }

    /**
     * 新增内容标签
     *
     * @param articleTag 内容标签
     * @return 结果
     */
    @Override
    public int insertArticleTag(ArticleTag articleTag) {
        articleTag.setCreateTime(DateUtils.getNowDate());
        articleTag.setCreateBy(ShiroUtils.getLoginName());
        if (articleTag.getParentTagId() == null) {
            articleTag.setParentTagId(0L);
        }
        return articleTagMapper.insertArticleTag(articleTag);
    }

    /**
     * 修改内容标签
     *
     * @param articleTag 内容标签
     * @return 结果
     */
    @Override
    public int updateArticleTag(ArticleTag articleTag) {
        articleTag.setUpdateTime(DateUtils.getNowDate());
        return articleTagMapper.updateArticleTag(articleTag);
    }

    /**
     * 删除内容标签对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteArticleTagByIds(String ids) {
        return articleTagMapper.deleteArticleTagByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除内容标签信息
     *
     * @param tagId 内容标签ID
     * @return 结果
     */
    @Override
    public int deleteArticleTagById(Long tagId) {
        return articleTagMapper.deleteArticleTagById(tagId);
    }

    /**
     * 查询内容标签树列表
     *
     * @return 所有内容标签信息
     */
    @Override
    public List<Ztree> selectArticleTagTree() {
        List<ArticleTag> articleTagList = articleTagMapper.selectArticleTagList(new ArticleTag());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (ArticleTag articleTag : articleTagList) {
            Ztree ztree = new Ztree();
            ztree.setId(articleTag.getTagId());
            ztree.setpId(articleTag.getParentTagId());
            ztree.setName(articleTag.getName());
            ztree.setTitle(articleTag.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
